// 03myCenter/02sectorization/04favorite/favorite.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var _this

Page({

    /*页面的初始数据*/
    data: {
        favorite: [],
        //删除
        thisTrue: [],
        // 单选框
        isCheck: [],
        // 全选框
        checkAll: false,
        // 点击管理
        manage: '管理',
        manageCheck: false,

    },

    /*生命周期函数--监听页面加载*/
    onLoad: function (options) {
        _this = this
        this.getFavorite()
    },
    // 获取收藏夹信息
    getFavorite: function () {
        var param = {
            User_ID: app.Data.userInfo.User_ID
        }
        co.getShareApi('favourite/Famg/getFavourite', param, function (res) {
            console.log("【获取收藏夹信息】===", res.data)
            if (res.code == 0) {
                var t = _this.data.thisTrue
                var c = _this.data.isCheck
                for (var i = 0; i < res.data.length; i++) {
                    t.push(0)
                    c.push(false)
                }
                _this.setData({
                    favorite: res.data,
                    thisTrue: t,
                    isCheck: c
                })
            }
        })
    },
    //长按弹出删除框
    deleteThis: function (e) {
        this.deleteClose(e.currentTarget.id, 1) 
    },
    // 点击隐藏删除框
    closeThis: function(e){
        this.deleteClose(e.currentTarget.id,0) 
    },
    deleteClose(index,count){
        var thisTrue = 'thisTrue[' + index + ']'
        this.setData({
            [thisTrue]: count
        })
    },
    // 管理按钮
    manageShops: function () {
        var manage, manageCheck, checkAll, check
        var check = this.data.isCheck
        if (this.data.manage == '管理') {
            manage = '完成'
            manageCheck = true
            checkAll = false
            for (var i = 0; i < check.length; i++) {
                check[i] = false
            }
        } else {
            manage = '管理'
            manageCheck = false
        }
        this.setData({
            manage: manage,
            manageCheck: manageCheck,
            checkAll: checkAll,
            isCheck: check
        })
    },
    // 全选按钮
    checkAll: function () {
        var c
        var checkAll = this.data.checkAll
        var checkone = this.data.isCheck
        if (checkAll == false) {
            c = true
            for (var i = 0; i < checkone.length; i++) {
                checkone[i] = true
            }
        } else {
            c = false
            for (var i = 0; i < checkone.length; i++) {
                checkone[i] = false
            }
        }
        this.setData({
            checkAll: c,
            isCheck: checkone
        })
    },
    // 单选框出现或没有时点击
    intoOrSelect: function (e) {
        var i = e.currentTarget.id
        var manageCheck = this.data.manageCheck
        var checks = this.data.isCheck
        // console.log("【获取传递ID】===", e.currentTarget.id)
        if (manageCheck != false) { //单选框出现
            if (checks[i] == false){
                checks[i] = true
            }else{
                checks[i] = false
            }
        }
        this.setData({
            isCheck: checks
        })
    },
    //删除
    deleteNow: function (e) {
        var id = e.currentTarget.id
        var param = {
            User_ID: app.Data.userInfo.User_ID,
            Products_ID: id
        }
        co.getShareApi('favourite/Famg/deleteFavourite',param,function(res){
            if(res.code == 0){
                _this.getFavorite()
                if (_this.data.favorite.length == 1){
                    wx.navigateBack({})
                }
            }
        })
    },
    // 删除全部
    deleteAll: function () {

    },

})